//
//  JFMainTabBar.swift
//  iOS_Swift_WeiBo
//
//  Created by huang on 16/3/19.
//  Copyright © 2016年 huang. All rights reserved.
//

import UIKit

let tabBarCount = 5

class JFMainTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算tabBar的宽度
        let width = frame.width / CGFloat(tabBarCount)
        
        // 令下标为0
        var index = 0
        
        // 遍历subviews
        for view in subviews {
            // 判断是否为UITabBarButton
            let btnClass = NSClassFromString("UITabBarButton")
            if view.isKindOfClass(btnClass!) {
                view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: frame.height)
                
                index += index == 1 ? 2 : 1
            }
            
        }
        // 添加加号按钮
        addButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    
    // MARK: 懒加载加号按钮
    private lazy var addButton:UIButton = {
        let button = UIButton()
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        
        return button
    }()
}
